package com.eqdd.yiqidian.ui.gongneng.car;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.io.IOException;

import javax.annotation.Nullable;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;

/**
 * @author吕志豪 .
 * @date 17-11-20  上午11:36.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class AddCarActivity extends SlimBaseMultiEditActivity {

    private RecyclerViewCustom dataBinding;
    @ToSave
    RichTextResult richTextResult;

    @Override
    public void initBinding() {
        /**
         * plateNumber（车牌号）
         VIN：车辆识别码
         engineCode：发动机号码
         vehicleType：汽车类型
         color：车辆颜色
         seats：汽车座位数（除司机外）
         purchaseDate：购买日期
         annInspectDate：年检日期
         insuranceEndDate：保险到期日期
         purchasePrice：购买价格
         */
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "添加车辆");
        initTopRightText("确定", v -> {
            if (checkParam()) {
                PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_CAR)
                        .tag(AddCarActivity.this)
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("plateNumber", setParam(0))
                        .params("vehicleType", setParam(1))
                        .params("seats", setParam(2))
                        .params("color", setParam(3))
                        .params("VIN", setParam(4))
                        .params("engineCode", setParam(5))
                        .params("purchaseDate", setParam(6))
                        .params("purchasePrice", setParam(7))
                        .params("insuranceEndDate", setParam(8))
                        .params("annInspectDate", setParam(9))
                        .params("remark", richTextResult == null ? " " : richTextResult.getContent());
                if (richTextResult != null) {
                    for (int i = 0; i < richTextResult.getSelectMedia().size(); i++) {
                        params.params("file" + i, new File(richTextResult.getSelectMedia().get(i).getCompressPath()));
                    }
                }
                params.execute(new DialogCallBack<HttpResult>(AddCarActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            clearData();
                            setResult(Config.SUCCESS);
                            finish();
                        }
                    }
                });
            }

        });
    }

    @Override
    protected void onDestroy() {
        OkGo.getInstance().cancelTag(this);
        super.onDestroy();
    }

    private boolean checkParam() {

        return true;
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 10:
                Bundle bundle = new Bundle();
                bundle.putString(Config.TITLE, "备注");
                bundle.putSerializable(Config.RICH_TEXT_RESULT, richTextResult);
                startActivityForResult(SelectRichTextActivity.class, bundle, Config.SELECT_RICH_TEXT);
                break;
        }
    }

    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("车牌号码", "", 0, "请输入"));
        datas.add(new SlimEditEnterBean("车辆类型", "", 1, "请输入"));
        datas.add(new SlimEditEnterBean("座位数(除司机)", "", 2, "请输入"));
        datas.add(new SlimEditEnterBean("颜色", "", 3, "请输入"));
        datas.add(new SlimEditEnterBean("车辆识别号", "", 4, "请输入"));
        datas.add(new SlimEditEnterBean("发动机号码", "", 5, "请输入"));
        datas.add(new SlimEditTimeBean("购买日期", "", 6, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditEnterBean("购买价格", "", 7, "请输入").addSchemes(StaticScheme.Numeric()).setTip("单位元"));
        datas.add(new SlimEditTimeBean("保险到期时间", "", 8, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditTimeBean("年检日期", "", 9, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditEnterBean("备注", "", 10, true, "请输入"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.SELECT_RICH_TEXT && resultCode == Config.SUCCESS) {
            richTextResult = (RichTextResult) data.getSerializableExtra(Config.RICH_TEXT_RESULT);
            datas.get(10).setContent(richTextResult.getContent());
            slimAdapterEx.notifyItemChanged(10);
        }

    }
}
